package com.sayai.excel.paln.makeplan;

import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.handler.AbstractSheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * @ClassName MyMergeStrategy
 * @Description TODO
 * @Author Sayai
 * @Date 2020/8/17 21:24
 * @Vertion 1.0
 **/
public class MyMergeStrategy extends AbstractRowWriteHandler {
    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                Integer relativeRowIndex, Boolean isHead) {


        if (relativeRowIndex >= 3) { // 固定行后，才开始进行纵向单元格合并处理
            System.err.println("afterRowDispose 第一列值 " + row.getCell(0).toString());
            System.err.println("afterRowDispose 第二列值 " + row.getCell(1).toString());
            Sheet sheet = writeSheetHolder.getSheet();

            // 当前值
            String nowName = sheet.getRow(relativeRowIndex + 1).getCell(0).toString();
            // 上一值
            String preName = sheet.getRow(relativeRowIndex).getCell(0).toString();

            // 处理A列
            if (!nowName.equals(preName) && relativeRowIndex > 3) {
                // 获取上方最后一个相同值的节点行数
                int firstnum = getUpFirstCell(relativeRowIndex - 1, sheet);


                System.err.println("开始纵坐标" + firstnum);
                System.err.println("结束纵坐标" + (relativeRowIndex - 1));

                CellRangeAddress cellRangeAddress =
                        new CellRangeAddress(firstnum + 1, relativeRowIndex, 0, 0);
                writeSheetHolder.getSheet().addMergedRegionUnsafe(cellRangeAddress);
            }

            // 当前值
            String nowNameB = sheet.getRow(relativeRowIndex + 1).getCell(1).toString();
            // 上一值
            String preNameB = sheet.getRow(relativeRowIndex).getCell(1).toString();

            // 处理B列
            if (!nowNameB.equals(preNameB) && relativeRowIndex > 3) {
                // 获取上方最后一个相同值的节点行数
                int firstnum = getUpSecondCell(relativeRowIndex - 1, sheet);


                System.err.println("开始纵坐标" + firstnum);
                System.err.println("结束纵坐标" + (relativeRowIndex - 1));

                CellRangeAddress cellRangeAddress =
                        new CellRangeAddress(firstnum + 1, relativeRowIndex, 1, 1);
                writeSheetHolder.getSheet().addMergedRegionUnsafe(cellRangeAddress);
            }
        }
    }


    private int getUpFirstCell(int rownum, Sheet sheet) {
        // 当前值
        String firstColText = sheet.getRow(rownum + 1).getCell(0).toString();
        // 上一值
        System.err.println(firstColText);
        String firstColPreText = sheet.getRow(rownum).getCell(0).toString();

        if (firstColText.equals(firstColPreText))
            return getUpFirstCell(rownum - 1, sheet);
        return rownum;
    }

    private int getUpSecondCell(int rownum, Sheet sheet) {
        // 当前值
        String firstColText = sheet.getRow(rownum + 1).getCell(0).toString();
        String secondColText = sheet.getRow(rownum + 1).getCell(1).toString();
        // 上一值
        String firstColPreText = sheet.getRow(rownum).getCell(0).toString();
        String secondPreText = sheet.getRow(rownum).getCell(1).toString();

        if (firstColText.equals(firstColPreText) && secondColText.equals(secondPreText))
            return getUpSecondCell(rownum - 1, sheet);
        return rownum;
    }
}
